package com.his.struts.action;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.foods.FoodsDao;
import com.his.struts.dao.foods.FoodsImp;
import com.his.struts.form.CommentForm;
import com.his.struts.form.FoodForm;
import com.his.struts.util.AjaxUtil;
import com.his.struts.util.PageUtil;

/**
 * 食物类
 * 
 * @author mlxnle
 * 
 */
public class FoodsAction extends DispatchAction {
	FoodsDao foodsDao;
	Logger log = Logger.getLogger(FoodsAction.class);

	/**
	 * 获得食物列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getFoodlist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ArrayList<FoodForm> foodlist = new ArrayList<FoodForm>();
		foodsDao = new FoodsImp();
		// 接收请求列表的类型
		String type11 = request.getParameter("type2");
		int ftype = Integer.parseInt(type11);
		foodlist = foodsDao.getFoodList(ftype);
		AjaxUtil.send(response, foodlist);
		return null;
	}

	/**
	 * 获得后台食品列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward allFoodlist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;
		String suptype = request.getParameter("suptype");
		request.setAttribute("suptype", suptype);
		getFoods(request, suptype);
		forward = mapping.findForward("bgfood");
		return forward;
	}

	/**
	 * 当前食物信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward currFood(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		foodsDao = new FoodsImp();
		// 当前食物id
		String sid = request.getParameter("fid");
		if (sid != null) {
			int fid = Integer.parseInt(sid);
			// 根据当前食物id取对象
			FoodForm foodForm = foodsDao.getFood(fid);
			AjaxUtil.send(response, foodForm);
		}
		return null;
	}

	/**
	 * 后台编辑食物信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getEditFood(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;
		foodsDao = new FoodsImp();
		// 当前食物id
		String sid = request.getParameter("fid");
		if (null != sid) {
			int fid = Integer.parseInt(sid);
			FoodForm foodForm = foodsDao.getFood(fid);
			request.setAttribute("editfoodForm", foodForm);
		}
		forward = mapping.findForward("editfood");
		return forward;
	}

	/**
	 * 后台编辑食物信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editFood(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		FoodForm food = (FoodForm) form;
		ActionForward forward = null;
		foodsDao = new FoodsImp();
		// 判断图片是否有改动
		if (food.getImgfile().getFileSize() != 0) {
			String imgname = food.getImgfile().getFileName();
			// 设置图片保存路径
			food.setFimguil("images/foods/" + imgname);
			String imgurl = request.getRealPath("");
			try {
				// 读写图片文件
				InputStream input = food.getImgfile().getInputStream();
				OutputStream output = new FileOutputStream(imgurl + "/"
						+ food.getFimguil());
				IOUtils.copy(input, output);
				input.close();
				output.close();
			} catch (IOException e) {
				log.error(e.getMessage());
			}
			foodsDao.updateFimguil(food);
		}
		// 提交其他信息修改
		foodsDao.updateFoodInfo(food);
		getFoods(request, food.getFtypecname());
		forward = mapping.findForward("bgfood");
		request.setAttribute("message", "修改成功");
		return forward;
	}

	/**
	 * 添加食物
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addFood(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward = null;
		FoodForm food = (FoodForm) form;
		foodsDao = new FoodsImp();
		// 判断图片是否有改动
		if (food.getImgfile().getFileSize() != 0) {
			String imgname = food.getImgfile().getFileName();
			// 设置图片保存路径
			food.setFimguil("images/foods/" + imgname);
			String imgurl = request.getRealPath("");
			try {
				// 读写图片文件
				InputStream input = food.getImgfile().getInputStream();
				OutputStream output = new FileOutputStream(imgurl + "/"
						+ food.getFimguil());
				IOUtils.copy(input, output);
				input.close();
				output.close();
			} catch (IOException e) {
				log.error(e.getMessage());
			}
			foodsDao.addFood(food);
		}
		getFoods(request, food.getFtypecname());
		forward = mapping.findForward("bgfood");
		request.setAttribute("message", "添加成功");
		return forward;
	}

	/**
	 * 相关食物
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward relFood(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		foodsDao = new FoodsImp();
		// 请求中获得食物fid
		String sid = request.getParameter("fid");
		if (sid != null) {
			int fid = Integer.parseInt(sid);
			ArrayList<String> rellist = new ArrayList<String>();
			// 根据食物fid取相关食物
			rellist = foodsDao.getRelFoods(fid);
			AjaxUtil.send(response, rellist);
		}
		return null;
	}

	/**
	 * 删除食物
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteFood(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;
		// 请求中获得食物id
		String ffid = request.getParameter("ffid");
		if (ffid != null) {
			int fid = Integer.parseInt(ffid);
			String suftp = request.getParameter("suftp");
			foodsDao = new FoodsImp();
			// 根据食物id将其删除
			foodsDao.deleteFood(fid);
			getFoods(request, suftp);
			forward = mapping.findForward("bgfood");
		}
		request.setAttribute("message", "删除成功");
		return forward;
	}

	/**
	 * 根据食物类型获得相关食物列表
	 * 
	 * @param request
	 * @param forwhat
	 */
	public void getFoods(HttpServletRequest request, String forwhat) {
		ArrayList<FoodForm> foodlist = new ArrayList<FoodForm>();
		foodsDao = new FoodsImp();
		String sql = "foodhealth left join foodtype "
			+ "on tpid = ftype  where  ftype in (select tpid from foodtype where styname ='"+forwhat+"')";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 20, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			foodlist = foodsDao.getAllFood(rs);
			// 数据放到request中
			request.setAttribute("foodlists", foodlist);
		}
	}

}
